package com.lishu.carsafe.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.model.entity.StatisticValue;

import java.util.ArrayList;
import java.util.List;

public class DayGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<StatisticValue> valueList = new ArrayList<>();
    private int type;

    public DayGridViewAdapter(Context context, int type) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        this.type = type;
    }

    public void setData(List<StatisticValue> valueList){
        this.valueList = valueList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return valueList.size();
    }

    @Override
    public Object getItem(int position) {
        if (valueList.size() > 0) {
            return valueList.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(view == null) {
            viewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.adapter_day_gridview, null);
            viewHolder.dayTV = view.findViewById(R.id.day);
            viewHolder.valueTV = view.findViewById(R.id.value);
            viewHolder.dayLayout = view.findViewById(R.id.day_layout);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        StatisticValue statisticValue = valueList.get(position);
        int dayValue = statisticValue.getDay();
        if(dayValue == 0){
            viewHolder.dayLayout.setVisibility(View.INVISIBLE);
        }else{
            viewHolder.dayLayout.setVisibility(View.VISIBLE);
            viewHolder.dayTV.setText("" + statisticValue.getDay());
            viewHolder.valueTV.setText(statisticValue.getValue());
        }
        if(type == 2){
            viewHolder.valueTV.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_yellow));//#FF9C5A
        }else if(type == 3){
            viewHolder.valueTV.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_blue));//#7C64FF
        }else{
            viewHolder.valueTV.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
        }

        return view;
    }

    class ViewHolder {
        TextView dayTV;
        TextView valueTV;
        LinearLayout dayLayout;
    }
    
}
